package cn.xiehangs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.StretchViewport;

import java.util.Random;

import static java.awt.image.ImageObserver.HEIGHT;
import static java.awt.image.ImageObserver.WIDTH;

public class game_main implements Screen , jie_mian {

    String name="game_main";//用于判断当前是哪个界面

    String next="";//用于判断下一个界面是哪个
    Texture piece1,piece2,piece3,piece4,imageUp,imageDown;
    OrthographicCamera cam;
    di_tu di_tu1;
    tou_zi tou_zi1;
    qi_zi qi_zi0,qi_zi1,qi_zi2,qi_zi3,qi_zi4,qi_zi5,qi_zi6,qi_zi7,qi_zi8,qi_zi9,qi_zi10,qi_zi11,qi_zi12,qi_zi13,qi_zi14,qi_zi15;
    qi_zi_group group0,group1,group2,group3;
    qi_pan_wz pan_wz;
    Stage stage;
    Button btn;
    SpriteBatch batch;
    Button.ButtonStyle style;
    聊天框 聊天框1;
    int stageWidth = 1024+1024/3;
    int stageHeight = 1024;
    Rect_a rect_a=new Rect_a();
    Random rd=new Random();
    boolean a=true;

    飞行棋 zhu_jie_mian;

    public game_main(飞行棋 zhu_jie_mian){
        this.zhu_jie_mian=zhu_jie_mian;
    }

    @Override
    public void show() {
        batch = new SpriteBatch();
        stage = new Stage(new StretchViewport(stageWidth,stageHeight));

        cam = new OrthographicCamera(WIDTH, HEIGHT);
        cam.setToOrtho(false);
        batch.setProjectionMatrix(cam.combined);
        di_tu1= new di_tu("飞行棋地图_红.png",1024,1024);
        聊天框1=new 聊天框("我");
        //判断是否鼠标在聊天框上

        stage.addActor(聊天框1.chat);
        stage.addActor(聊天框1.send);

        tou_zi1=new tou_zi(1024,1024*2/3,1024/3,1024/3,"tou_zi1","tou_zi");
        stage.addActor(tou_zi1);
        qi_zi0=new qi_zi("hong",0);
        qi_zi1=new qi_zi("hong",1);
        qi_zi2=new qi_zi("hong",2);
        qi_zi3=new qi_zi("hong",3);
        qi_zi4=new qi_zi("lan",4);
        qi_zi5=new qi_zi("lan",5);
        qi_zi6=new qi_zi("lan",6);
        qi_zi7=new qi_zi("lan",7);
        qi_zi8=new qi_zi("huang",8);
        qi_zi9=new qi_zi("huang",9);
        qi_zi10=new qi_zi("huang",10);
        qi_zi11=new qi_zi("huang",11);
        qi_zi12=new qi_zi("lv",12);
        qi_zi13=new qi_zi("lv",13);
        qi_zi14=new qi_zi("lv",14);
        qi_zi15=new qi_zi("lv",15);

        Button.ButtonStyle style1 = new Button.ButtonStyle();
        Texture imageUp1 = new Texture("button/返回菜单_up.png");
        Texture imageover1 = new Texture("button/返回菜单_down.png");
        style1.up = new TextureRegionDrawable(new TextureRegion(imageUp1));
        style1.over = new TextureRegionDrawable(new TextureRegion(imageover1));
        style1.down = new TextureRegionDrawable(new TextureRegion(imageUp1));

        btn = new Button(style1);

        btn.setPosition(1024+1024/6-65,570);

        btn.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                next="start";
                zhu_jie_mian.panduan();
            }
        });

        stage.addActor(btn);





        //下面顺序不能变，是按先后顺序绘制的
        stage.addActor(di_tu1);//添加地图
//        stage.addActor(qi_zi0);//添加棋子
//        stage.addActor(qi_zi1);
//        stage.addActor(qi_zi2);
//        stage.addActor(qi_zi3);
//        stage.addActor(qi_zi4);
//        stage.addActor(qi_zi5);
//        stage.addActor(qi_zi6);
//        stage.addActor(qi_zi7);
//        stage.addActor(qi_zi8);
//        stage.addActor(qi_zi9);
//        stage.addActor(qi_zi10);
//        stage.addActor(qi_zi11);
//        stage.addActor(qi_zi12);
//        stage.addActor(qi_zi13);
//        stage.addActor(qi_zi14);
//        stage.addActor(qi_zi15);
//        group0=new qi_zi_group(0,qi_zi0,qi_zi1,qi_zi2,qi_zi3);
//        group1=new qi_zi_group(1,qi_zi4,qi_zi5,qi_zi6,qi_zi7);
//        group2=new qi_zi_group(2,qi_zi8,qi_zi9,qi_zi10,qi_zi11);
//        group3=new qi_zi_group(3,qi_zi12,qi_zi13,qi_zi14,qi_zi15);
        group0=new qi_zi_group(0,qi_zi0,qi_zi1,qi_zi2,qi_zi3,false);
        group1=new qi_zi_group(1,qi_zi4,qi_zi5,qi_zi6,qi_zi7,false);
        group2=new qi_zi_group(2,qi_zi8,qi_zi9,qi_zi10,qi_zi11,false);
        group3=new qi_zi_group(3,qi_zi12,qi_zi13,qi_zi14,qi_zi15,false);
        group0.set聊天框_1(聊天框1);
        group1.set聊天框_1(聊天框1);
        group2.set聊天框_1(聊天框1);
        group3.set聊天框_1(聊天框1);
        qi_zi_group.tou_zi=tou_zi1;
        stage.addActor(group0);
        stage.addActor(group1);
        stage.addActor(group2);
        stage.addActor(group3);
        pan_wz=new qi_pan_wz(group0,group1,group2,group3);
        group0.number_1.setMove_step_pool(0);

        InputMultiplexer inputMultiplexer= new InputMultiplexer();//创建一个InputMultiplexer实例
        Gdx.input.setInputProcessor(inputMultiplexer);//将inputMultiplexer设置为输入处理器
        inputMultiplexer.addProcessor(stage);//添加舞台为输入处理器
        Gdx.graphics.setWindowedMode(stageWidth,stageHeight);
    }
    public void setRobot(boolean is_robot_1,boolean is_robot_2,boolean is_robot_3,boolean is_robot_4){
        group0.is_robot=is_robot_1;
        group1.is_robot=is_robot_2;
        group2.is_robot=is_robot_3;
        group3.is_robot=is_robot_4;
    }

    @Override
    public void render(float delta) {
            Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);// 设置清屏颜色
            Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);// 清屏
            batch.begin();//共享资源绘制
            batch.end();
            group0.robot();
            group1.robot();
            group2.robot();
            group3.robot();
            group0.is_win();
            group1.is_win();
            group2.is_win();
            group3.is_win();
            stage.act();//舞台动作
            stage.draw();//舞台绘制
            chu_pen(rect_a);

    }

    @Override
    public void resize(int width, int height) {
//        if(a){
//            cam.setToOrtho(false,stageWidth,stageHeight);
//            a=false;
//        }
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        piece1.dispose();
        piece2.dispose();
        piece3.dispose();
        piece4.dispose();
        stage.dispose();
        batch.dispose();



    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public String getNext() {
        return null;
    }
    void  chu_pen(Rect_a rect){
        int x=Gdx.input.getX();
        int y=Gdx.graphics.getHeight() - Gdx.input.getY();

        if (x > rect.x && x < rect.x + rect.width && y > rect.y &&y < rect.y + rect.height) {
            聊天框1.setflag(true);
        }
        else {
            聊天框1.setflag(false);
        }
    }
    private class Rect_a {
        public int stageWidth = 1024 / 3, stageHeight = 512;
        public int stageHeight_chat_screen = 50;
        int x = 1024;
        int y = 0;
        int width = stageWidth;
        int height = stageHeight+stageHeight_chat_screen;
        Rect_a() {
        }

        Rect_a(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
    }
    public void chu_shi_hua(){
        group0.chu_shi_hua();
        group1.chu_shi_hua();
        group2.chu_shi_hua();
        group3.chu_shi_hua();
        tou_zi1.chu_shi_hua();
        qi_zi.move_step_pool=0;
        qi_zi.is_move_pool=true;
    }
}
